import Vue from 'vue'
import VueRouter from 'vue-router'
import yyh from './yyh'
import lry from './lry'
import store from '../store/index.js'

Vue.use(VueRouter)

const routes = [
 ...yyh,
 ...lry
  
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{ //路由的全局前置守卫
//   console.log(to,from);
  // path = '/checkout'   '/account'
  if(to.path.indexOf('/cart')==-1&&to.path.indexOf('/user')==-1&&to.path.indexOf('/detail')==-1){ //判断用户访问的组件
      next()  //放行操作
  }else{
      if(store.state.my.userInfo){ //判断登录状态
          next()
      }else{
          next('/login')
      }
  }
})


const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err);
};
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

export default router
